#### this is a replication code for "IGO Networks and Convergence in Domestic Economic Policies. 
#### Wednesday, 26.November.2008
#### by Xun Cao

library(xtable)

dd <- c("C:/XunCao/Research/Desert/IGO_f_i/") # you should set up your own working directory which is where you saved the data.
setwd(dd)

### read in data.
Yx<-dget("Yx") 

### variable names:
# "year": year  
# "cty1"/"cty2": acronyms for the two countries in the dyad
# "pdist": overall policy distance
# "Gsize.dist": distance in the policy for the size of the government
# "Smony.dist": distance in the policy for monetary policies
# "Regul.dist": distance in the policy for regulation policies
# "igo.sm": shared number of IGOs with social/cultural functions and minimalist institutional capacity
# "igo.em": shared number of IGOs with economic functions and minimalist institutional capacity
# "igo.ss": shared number of IGOs with social/cultural functions and structured institutional capacity
# "igo.es": shared number of IGOs with economic functions and structured institutional capacity
# "igo.ei": shared number of IGOs with economic functions and interventionist institutional capacity
# "eu": shared EEC/EU membership, this is binary (0/1)
# "oecd": shared OECD membership, this is binary (0/1)
# "wto": shared GATT/WTO membership, this is binary (0/1)
# "se.export": structural equivalence in exports
# "trade": shared trade
# "distance": distance between capital cities
# "cl": shared language, this is binary (0/1)
# "igo": shared number of IGOs regardless of functions and institutional capacity
# "lg": five lag of "pdist" (overall policy distance)
# "lg.s": five lag of "Gsize.dist" (distance in the policy for the size of the government)
# "lg.m": five lag of "Smony.dist" (distance in the policy for monetary policies)  
# "lg.r": five lag of "Regul.dist" (distance in the policy for regulation policies).

# Table 5: basic correlation statistics:
xtable(round(cor(cbind(Yx$pdist, Yx$Gsize.dist, Yx$Smony.dist, Yx$Regul.dist, 
                       Yx$igo, Yx$igo.sm,  Yx$igo.em,  Yx$wto,  Yx$eu,  Yx$oecd,  
                       Yx$se.export,  log(Yx$trade+.1), Yx$distance, Yx$cl)), digits=c(rep(2, 16))))

# Table 6: IGO connections disaggregated, 1990, 1995, and 2000. 
# full model
fit.lm<-lm(pdist~ igo.sm
                 +igo.em
                 +eu
                 +wto
                 +oecd
                 +se.export
                 +log(trade+.1)
                 +log(distance+.1)
                 +cl
                 +lg
                 +as.factor(year) 
                 , data=Yx)
summary(fit.lm)

# IGO: social, minimalist
fit.lm<-lm(pdist~ igo.sm
                 #+igo.em
                 #+eu
                 #+wto
                 #+oecd
                 +se.export
                 +log(trade+.1)
                 +log(distance+.1)
                 +cl
                 +lg
                 +as.factor(year) 
                 , data=Yx)
summary(fit.lm)

# IGO: economic, minimalist
fit.lm<-lm(pdist~ # igo.sm
                 +igo.em
                 #+eu
                 #+wto
                 #+oecd
                 +se.export
                 +log(trade+.1)
                 +log(distance+.1)
                 +cl
                 +lg
                 +as.factor(year) 
                 , data=Yx)
summary(fit.lm)

# IGO: economic, interventionist
fit.lm<-lm(pdist~ # igo.sm
                 #+igo.em
                 +eu
                 +wto
                 +oecd
                 +se.export
                 +log(trade+.1)
                 +log(distance+.1)
                 +cl
                 +lg
                 +as.factor(year) 
                 , data=Yx)
summary(fit.lm)


# regressions for Table 4/Figure 2 in which overall policy distance is disaggregated into three policy spaces:
# size of the government:
fit.lm<-lm(Gsize.dist~ igo.sm
                  +igo.em
                  +eu
                  +wto
                  +oecd
                  +se.export
                  +log(trade+.1)
                  +log(distance+.1)
                  +cl
                  +lg.s
                  +as.factor(year)
                 , data=Yx
                  )
summary(fit.lm)

# monetary policies:
fit.lm<-lm(Smony.dist~ igo.sm
                  +igo.em
                  +eu
                  +wto
                  +oecd
                  +se.export
                  +log(trade+.1)
                  +log(distance+.1)
                  +cl
                  +lg.m
                  +as.factor(year)
                 , data=Yx
                  )
summary(fit.lm)

# monetary policies:
fit.lm<-lm(Regul.dist~ igo.sm
                  +igo.em
                  +eu
                  +wto
                  +oecd
                  +se.export
                  +log(trade+.1)
                  +log(distance+.1)
                  +cl
                  +lg.r
                  +as.factor(year)
                 , data=Yx
                  )
summary(fit.lm)




#### Matching in the section "Endogeneity Problem and Caual Modeling":
library(MatchIt)

yx<-Yx
yx<-cbind(subset(yx, yx$year>=1995),  subset(yx, yx$year<=1995, select=c("year", "cty1", "cty2", "igo.sm", "igo.em", "eu", "oecd", "wto", "se.export", "trade", "igo")))
colnames(yx)<-c("year", "cty1", "cty2", 
                "pdist", "Gsize.dist", "Smony.dist", "Regul.dist", 
                "igo.sm", "igo.em", "igo.ss", "igo.es", "igo.ei", "eu", "oecd", "wto", "se.export", "trade", "distance", "cl", "igo",
                "lg", "lg.s", "lg.m", "lg.r", 
                "year.t5", "cty1.t5", "cty2.t5",
                "igo.sm.t5", "igo.em.t5", "eu.t5", "oecd.t5", "wto.t5", "se.export.t5", "trade.t5", "igo.t5") 
                # notice that those variable with ".t5" is the lagged IVs, we lag them by 5 years to deal with, to some 
                # extent, the reverse causality issue.  
                                                                        
N<-M<-0
for (i in 1:dim(yx)[1]){if (as.character(yx$cty1[i])!=as.character(yx$cty1.t5[i])){N<-N+1}
                        if (as.character(yx$cty2[i])!=as.character(yx$cty2.t5[i])){M<-M+1}}
                        
### Causal effect of OECD: 
Coef<-NULL
yx2<-yx
m.out <- matchit(oecd.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5 + wto.t5 + eu.t5  + trade.t5 + distance + cl + lg, method="nearest", data = yx2)
summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(pdist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[7,])

yx2<-yx
m.out <- matchit(oecd.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5 + wto.t5 + eu.t5  + trade.t5 + distance + cl + lg.s, method="nearest", data = yx2)
summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Gsize.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.s + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[7,])


yx2<-yx
m.out <- matchit(oecd.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5 + wto.t5 + eu.t5  + trade.t5 + distance + cl + lg.m, method="nearest", data = yx2)
summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Smony.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.m + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[7,])


yx2<-yx
m.out <- matchit(oecd.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5 + wto.t5 + eu.t5  + trade.t5 + distance + cl + lg.r, method="nearest", data = yx2)
summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Regul.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.r + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[7,])

# see the causal effect of the OECD on overall policy distance (first row in Coef), size of the government (second row), monetary policies (third), and regulation policies (last). 
Coef


### EU:
Coef<-NULL
yx2<-yx
m.out <- matchit(eu.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5 + wto.t5 + oecd.t5  + trade.t5 + distance + cl + lg, method="nearest", data = yx2)
# summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(pdist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[6,])



yx2<-yx
m.out <- matchit(eu.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5 + wto.t5 + oecd.t5  + trade.t5 + distance + cl + lg.s, method="nearest", data = yx2)
summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Gsize.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.s + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[6,])

yx2<-yx
m.out <- matchit(eu.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5 + wto.t5 + oecd.t5  + trade.t5 + distance + cl + lg.m, method="nearest", data = yx2)
summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Smony.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.m + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[6,])

yx2<-yx
m.out <- matchit(eu.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5 + wto.t5 + oecd.t5  + trade.t5 + distance + cl + lg.r, method="optimal",  data = yx2)
summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Regul.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.r + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[6,])



### wto:
Coef<-NULL
yx2<-yx
m.out <- matchit(wto.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5  + eu.t5 + oecd.t5 + trade.t5 + distance + cl + lg, method="nearest", replace = TRUE, data = yx2)
summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(pdist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[5,])

yx2<-yx
m.out <- matchit(wto.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5 + eu.t5 + oecd.t5  + trade.t5 + distance + cl + lg.s, method="nearest", replace = TRUE,data = yx2)
summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Gsize.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.s + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[5,])

yx2<-yx
m.out <- matchit(wto.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5 + eu.t5 + oecd.t5  + trade.t5 + distance + cl + lg.m, method="nearest", replace = TRUE,data = yx2)
summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Smony.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.m + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[5,])

yx2<-yx
m.out <- matchit(wto.t5 ~ se.export.t5 + igo.sm.t5 + igo.em.t5 + eu.t5 + oecd.t5  + trade.t5 + distance + cl + lg.r, method="nearest", replace = TRUE, data = yx2)
summary(m.out); plot(m.out);plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Regul.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.r + as.factor(year), data=m.data)
summary(fit.m)
Coef<-rbind(Coef, summary(fit.m)$coef[5,])



### igo.em: we try different thresholds:
COEF<-NULL
for (thres in c(5, 6, 7))
{
yx2<-yx
# thres<-5
yx2$igo.em.t5[yx2$igo.em.t5<thres]<-0
yx2$igo.em.t5[yx2$igo.em.t5>=1]<-1

Coef<-NULL

m.out <- matchit(igo.em.t5 ~ se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + trade.t5 + distance + cl + lg, method="nearest", data = yx2)
summary(m.out); # plot(m.out); plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(pdist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg + as.factor(year), data=m.data)
summary(fit.m)
Coef<-cbind(Coef, summary(fit.m)$coef[2,])

m.out <- matchit(igo.em.t5 ~ se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + trade.t5 + distance + cl + lg.s, method="nearest", data = yx2)
summary(m.out); # plot(m.out); plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Gsize.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.s + as.factor(year), data=m.data)
summary(fit.m)
Coef<-cbind(Coef, summary(fit.m)$coef[2,])

m.out <- matchit(igo.em.t5 ~ se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + trade.t5 + distance + cl + lg.m, method="nearest", data = yx2)
summary(m.out); # plot(m.out); plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Smony.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.m + as.factor(year), data=m.data)
summary(fit.m)
Coef<-cbind(Coef, summary(fit.m)$coef[2,])

m.out <- matchit(igo.em.t5 ~ se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + trade.t5 + distance + cl + lg.r, method="nearest", data = yx2)
summary(m.out); # plot(m.out); plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Regul.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.r + as.factor(year), data=m.data)
summary(fit.m)
Coef<-cbind(Coef, summary(fit.m)$coef[2,])
COEF<-rbind(COEF, Coef)
}




### igo.sm
COEF.s<-NULL
for (thres in c(5, 6, 7))
{
yx2<-yx
# thres<-6
yx2$igo.sm.t5[yx2$igo.sm.t5<thres]<-0
yx2$igo.sm.t5[yx2$igo.sm.t5>=1]<-1

Coef<-NULL

m.out <- matchit(igo.sm.t5 ~ se.export.t5 +igo.em.t5 + wto.t5 + eu.t5 + oecd.t5 + trade.t5 + distance + cl + lg, method="nearest", data = yx2)
summary(m.out); # plot(m.out); plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(pdist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg + as.factor(year), data=m.data)
summary(fit.m)
Coef<-cbind(Coef, summary(fit.m)$coef[4,])

m.out <- matchit(igo.sm.t5 ~ se.export.t5 +igo.em.t5 + wto.t5 + eu.t5 + oecd.t5 + trade.t5 + distance + cl + lg.s, method="nearest", data = yx2)
summary(m.out); # plot(m.out); plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Gsize.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.s + as.factor(year), data=m.data)
summary(fit.m)
Coef<-cbind(Coef, summary(fit.m)$coef[4,])

m.out <- matchit(igo.sm.t5 ~ se.export.t5 +igo.em.t5 + wto.t5 + eu.t5 + oecd.t5 + trade.t5 + distance + cl + lg.m, method="nearest", data = yx2)
summary(m.out); # plot(m.out); plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Smony.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.m + as.factor(year), data=m.data)
summary(fit.m)
Coef<-cbind(Coef, summary(fit.m)$coef[4,])

m.out <- matchit(igo.sm.t5 ~ se.export.t5 +igo.em.t5 + wto.t5 + eu.t5 + oecd.t5 + trade.t5 + distance + cl + lg.r, method="nearest", data = yx2)
summary(m.out); # plot(m.out); plot(m.out, type="jitter")
m.data <- match.data(m.out, distance="dist")
fit.m<-lm(Regul.dist~ igo.em.t5 + se.export.t5 +igo.sm.t5 + wto.t5 + eu.t5 + oecd.t5 + log(trade.t5 +.1) + log(distance + .1)  + cl + lg.r + as.factor(year), data=m.data)
summary(fit.m)
Coef<-cbind(Coef, summary(fit.m)$coef[4,])
COEF.s<-rbind(COEF.s, Coef)
}
